<?php
namespace app\store\controller\food\shop;

use app\store\controller\food\Controller;
use app\store\model\food\Qrcode as QrcodeModel;
use app\store\model\food\Shop as ShopModel;
use app\store\model\food\Table as TableModel;
use hema\wechat\Driver;
use think\facade\View;

/**
 * 二维码控制器
 */
class Qrcode extends Controller
{
     /**
     * 列表
     */
    public function index()
    {
        $wx = new Driver;
        $qrcode = $wx->getJumpQRCode($this->applet_id);
        $qrcode['rule'] = false;//是否添加了规则
        if($qrcode['errcode'] == 0){
            $data = [
                'is_edit' => 0,//0=新增，1=修改
                'prefix' => base_url() . 'food/',//规则
                'path' => 'pages/shop/index',//打开页面
                'open_version' => 3,//线上正式版
                'permit_sub_rule' => 1,//1=不占用，2=占用
            ];
            foreach ($qrcode['rule_list'] as $item){
                //判断规则
                if($item['prefix'] == $data['prefix']){
                    if($item['path'] == $data['path'] and $item['open_version'] == $data['open_version'] and $item['permit_sub_rule'] == $data['permit_sub_rule']){
                        $qrcode['rule'] = true;//添加了规则
                        break;
                    }else{
                        $data['is_edit'] = 1;
                    }
                }
            }
            if(!$qrcode['rule']){
                //增加或修改二维码规则
                if($wx->addJumpQRCode($this->applet_id,$data)){
                    //发布已设置的二维码规则
                    if($wx->publishJumpQRCode($this->applet_id,$data['prefix'])){
                        
                    }else{
                        $error = $wx->getError() ?: '发布二维码规则失败';
                        return $this->renderError($error);
                    }
                }else{
                    $error = $wx->getError() ?: '操作二维码规则失败';
                    return $this->renderError($error);
                }
            }
        }
        $model = new QrcodeModel;
        $list = $model->getList();
        return View::fetch('index', compact('list','qrcode'));
    }
    
    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $model = new QrcodeModel;
            if ($model->add($this->postData('data'))) {
                return $this->renderSuccess('添加成功', url('food.shop.qrcode/index'));
            }
            $error = $model->getError() ?: '添加失败';
            return $this->renderError($error);
        }
        return redirect(url('food.shop.qrcode/index'));
    }

    /**
     * 编辑
     */
    public function edit($id)
    {
        // 详情
        $model = QrcodeModel::detail($id);
        if (!$this->request->isAjax()) {
            $shopModel = new ShopModel;
            $shop = $shopModel->getList(false);//获取门店列表
            $table = [];
            //获取门店餐桌列表
            for($n=0;$n<sizeof($shop);$n++){
                $tableModel = new TableModel;
                $table[$shop[$n]['shop_id']] = $tableModel->getList($shop[$n]['shop_id']);
            }
            return View::fetch('edit', compact('model','shop','table'));
        }
        // 更新记录
        if ($model->edit($this->postData('data'))) {
            return $this->renderSuccess('操作成功', url('food.shop.qrcode/index'));
        }
        $error = $model->getError() ?: '操作失败';
        return $this->renderError($error);
    }

    /**
     * 删除
     */
    public function delete($id)
    {
        $model = QrcodeModel::get($id);
        if($model->remove()) {
            return $this->renderSuccess('删除成功');
        }
        $error = $model->getError() ?: '删除失败';
        return $this->renderError($error);
    }

}
